# Copyright 2021 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_python//python:proto.bzl", "py_proto_library")
load(
    "//pw_build:pigweed.bzl",
    "pw_cc_test",
    "pw_facade",
)
load("//pw_protobuf_compiler:pw_proto_library.bzl", "pw_proto_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

# The crash facade provides architecture-independent APIs that may be helpful
# for analysing the CPU state delegating the reporting and crashing to the
# backend.
constraint_setting(
    name = "crash_constraint_setting",
)

constraint_value(
    name = "entry_backend",
    constraint_setting = "//pw_cpu_exception:entry_constraint_setting",
)

constraint_value(
    name = "support_backend",
    constraint_setting = "//pw_cpu_exception:support_constraint_setting",
)

cc_library(
    name = "config",
    hdrs = ["pw_cpu_exception_cortex_m_private/config.h"],
    deps = [":config_override"],
)

label_flag(
    name = "config_override",
    build_setting_default = "//pw_build:default_module_config",
)

# Override-able flag for each facade backend.
label_flag(
    name = "crash_backend",
    build_setting_default = "//pw_build:unspecified_backend",
)

pw_facade(
    name = "crash",
    srcs = ["crash.cc"],
    hdrs = ["public/pw_cpu_exception_cortex_m/crash.h"],
    backend = ":crash_backend",
    includes = ["public"],
    target_compatible_with = select({
        "@platforms//cpu:armv7-m": [],
        "@platforms//cpu:armv7e-m": [],
        "@platforms//cpu:armv7e-mf": [],
        "@platforms//cpu:armv8-m": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":config",
        ":cortex_m_constants",
        ":cpu_state",
        "//pw_preprocessor:cortex_m",
    ],
)

cc_library(
    name = "cpu_state",
    hdrs = ["public/pw_cpu_exception_cortex_m/cpu_state.h"],
    includes = ["public"],
    deps = [
        "//pw_preprocessor",
        "//pw_preprocessor:cortex_m",
    ],
)

cc_library(
    name = "util",
    srcs = ["util.cc"],
    hdrs = ["public/pw_cpu_exception_cortex_m/util.h"],
    includes = ["public"],
    target_compatible_with = select({
        "@platforms//cpu:armv7-m": [],
        "@platforms//cpu:armv7e-m": [],
        "@platforms//cpu:armv7e-mf": [],
        "@platforms//cpu:armv8-m": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":config",
        ":cortex_m_constants",
        ":cpu_state",
        "//pw_log",
        "//pw_preprocessor:cortex_m",
    ],
)

cc_library(
    name = "support",
    srcs = ["support.cc"],
    deps = [
        ":config",
        ":cortex_m_constants",
        ":cpu_state",
        ":util",
        "//pw_cpu_exception:support.facade",
        "//pw_log",
        "//pw_preprocessor",
        "//pw_preprocessor:cortex_m",
        "//pw_string",
    ],
)

cc_library(
    name = "proto_dump",
    srcs = ["proto_dump.cc"],
    hdrs = ["public/pw_cpu_exception_cortex_m/proto_dump.h"],
    includes = ["public"],
    deps = [
        ":config",
        ":cpu_state",
        ":cpu_state_protos_cc.pwpb",
        ":support",
        "//pw_protobuf",
        "//pw_status",
        "//pw_stream",
    ],
)

proto_library(
    name = "cpu_state_protos",
    srcs = ["pw_cpu_exception_cortex_m_protos/cpu_state.proto"],
    import_prefix = "pw_cpu_exception_cortex_m_protos",
    strip_import_prefix = "/pw_cpu_exception_cortex_m/pw_cpu_exception_cortex_m_protos",
)

py_proto_library(
    name = "cpu_state_protos_pb2",
    deps = [":cpu_state_protos"],
)

pw_proto_library(
    name = "cpu_state_protos_cc",
    deps = [":cpu_state_protos"],
)

cc_library(
    name = "cpu_exception",
    hdrs = [
        "public_overrides/pw_cpu_exception_backend/state.h",
    ],
    includes = [
        "public_overrides",
    ],
    deps = [
        ":cpu_state",
        "//pw_cpu_exception:entry.facade",
        "//pw_preprocessor",
        "//pw_preprocessor:cortex_m",
    ],
)

cc_library(
    name = "cpu_exception_impl",
    srcs = ["entry.cc"],
    deps = [
        ":config",
        ":cortex_m_constants",
        ":cpu_state",
        ":util",
        # Depend on the full backends in the impl target.
        "//pw_cpu_exception:entry",
        "//pw_cpu_exception:handler",
    ],
)

cc_library(
    name = "snapshot",
    srcs = ["snapshot.cc"],
    hdrs = ["public/pw_cpu_exception_cortex_m/snapshot.h"],
    deps = [
        ":config",
        ":cortex_m_constants",
        ":cpu_state",
        ":cpu_state_protos_cc.pwpb",
        ":proto_dump",
        ":util",
        "//pw_log",
        "//pw_protobuf",
        "//pw_status",
        "//pw_thread:snapshot",
        "//pw_thread:thread_cc.pwpb",
    ],
)

cc_library(
    name = "cortex_m_constants",
    hdrs = ["pw_cpu_exception_cortex_m_private/cortex_m_constants.h"],
    visibility = ["//visibility:private"],
    deps = ["//pw_preprocessor:cortex_m"],
)

pw_cc_test(
    name = "cpu_exception_entry_test",
    srcs = [
        "exception_entry_test.cc",
    ],
    deps = [
        ":config",
        ":cpu_exception",
        ":cpu_state",
        "//pw_cpu_exception:handler",
    ],
)

pw_cc_test(
    name = "util_test",
    srcs = [
        "util_test.cc",
    ],
    deps = [
        ":cpu_state",
        ":util",
    ],
)

# This test defines a crash backend so one must not be specified.
cc_library(
    name = "crash_test_util",
    srcs = [
        "crash.cc",
    ],
    hdrs = [
        "private/pw_cpu_exception_cortex_m_backend/crash.h",
        "public/pw_cpu_exception_cortex_m/crash.h",
    ],
    visibility = ["//visibility:private"],
    deps = [
        ":config",
        ":cortex_m_constants",
        ":cpu_state",
        ":util",
        "//pw_cpu_exception:entry",
        "//pw_preprocessor",
        "//pw_preprocessor:cortex_m",
    ],
)

pw_cc_test(
    name = "crash_test",
    srcs = [
        "crash_test.cc",
    ],
    target_compatible_with = select({
        "@platforms//cpu:armv7-m": [],
        "@platforms//cpu:armv7e-m": [],
        "@platforms//cpu:armv7e-mf": [],
        "@platforms//cpu:armv8-m": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":config",
        ":cortex_m_constants",
        ":cpu_state",
        ":crash_test_util",
        ":util",
        "//pw_cpu_exception:entry",
        "//pw_preprocessor",
        "//pw_preprocessor:cortex_m",
        "//pw_status",
        "//pw_string:builder",
        "//pw_sync:lock_annotations",
        "//pw_sync:mutex",
    ],
)
